<?php
namespace App\Models;

use App\Exceptions\ApiException;
use Illuminate\Support\Facades\DB;

class NeighborHelp extends CommonModel {
	protected $table = 'neighbor_help';
	
	public static $type = [
		['type'=>0,'function'=>'shareCreate','name'=>'邻里分享'],
		['type'=>1,'function'=>'carpoolCreate','name'=>'拼车'],
		['type'=>2,'function'=>'rentingCreate','name'=>'租房'],
		['type'=>3,'function'=>'oldstuffCreate','name'=>'二手买卖'],
		['type'=>4,'function'=>'activityCreate','name'=>'社区活动'],
//		['type'=>5,'function'=>'otherCreate','name'=>'其他'],
	];
	
	// 公用方法
	public function scopeList($query){
		$data = $query->select(DB::raw('neighbor_help.id,neighbor_help.created_at,uid,title,description,comment_count,commend_count,username,avatar,u.mobile'))->leftJoin('client_user as u','neighbor_help.uid','=','u.id')->where(['neighbor_help.status'=>1])->orderBy('neighbor_help.id','desc');
		return $data;
	}
	
	/**
	 * @param $query
	 * @return mixed
	 * 邻里分享列表
	 */
	public function scopeShare($query,$keyword){
		$data = $query->where(['neighbor_help.type'=>0])->where(function($query) use ($keyword){
			$query->where('title','like','%'.$keyword.'%')->orWhere('description','like','%'.$keyword.'%');
		})->list();
		return $data;
	}
	
	// 邻里分享数量
	public function scopeShareCount($query){
		$data = $query->where(['type'=>0])->count();
		return $data;
	}
	
	// 邻里分享添加
	public function scopeShareCreate($query,$uid,$title,$description,$images,$comment_allow = 1){
		$help_id = $query->helpCreate($uid,$title,$description,$images,0,$comment_allow);
		return $help_id;
	}
	
	
	/**
	 * @param $query
	 * @return mixed
	 * 邻里拼车列表
	 */
	public function scopeCarpool($query,$keyword){
		$data = $query->where(['neighbor_help.type'=>1])->where(function($query) use ($keyword){
			$query->where('title','like','%'.$keyword.'%')->orWhere('description','like','%'.$keyword.'%');
		})->list();
		return $data;
	}
	
	// 邻里拼车数量
	public function scopeCarpoolCount($query){
		$data = $query->where(['type'=>1])->count();
		return $data;
	}
	
	// 邻里拼车添加
	public function scopeCarpoolCreate($query,$uid,$title,$description,$images,$comment_allow = 1){
		$help_id = $query->helpCreate($uid,$title,$description,$images,1,$comment_allow);
		return $help_id;
	}
	
	/**
	 * @param $query
	 * @return mixed
	 * 邻里租房列表
	 */
	public function scopeRenting($query,$keyword){
		$data = $query->where(['neighbor_help.type'=>2])->where(function($query) use ($keyword){
			$query->where('title','like','%'.$keyword.'%')->orWhere('description','like','%'.$keyword.'%');
		})->list();
		return $data;
	}
	
	// 邻里租房数量
	public function scopeRentingCount($query){
		$data = $query->where(['type'=>2])->count();
		return $data;
	}
	
	// 邻里租房添加
	public function scopeRentingCreate($query,$uid,$title,$description,$images,$comment_allow = 1){
		$help_id = $query->helpCreate($uid,$title,$description,$images,2,$comment_allow);
		return $help_id;
	}
	
	/**
	 * @param $query
	 * @return mixed
	 * 邻里二手买卖列表
	 */
	public function scopeOldstuff($query,$keyword){
		$data = $query->where(['neighbor_help.type'=>3])->where(function($query) use ($keyword){
			$query->where('title','like','%'.$keyword.'%')->orWhere('description','like','%'.$keyword.'%');
		})->list();
		return $data;
	}
	
	// 邻里二手买卖数量
	public function scopeOldstuffCount($query){
		$data = $query->where(['type'=>3])->count();
		return $data;
	}
	
	// 邻里二手买卖添加
	public function scopeOldstuffCreate($query,$uid,$title,$description,$images,$comment_allow = 1){
		$help_id = $query->helpCreate($uid,$title,$description,$images,3,$comment_allow);
		return $help_id;
	}
	
	
	/**
	 * @param $query
	 * @return mixed
	 * 社区活动
	 */
	public function scopeActivity($query,$keyword){
		$data = $query->where(['neighbor_help.type'=>4])->where(function($query) use ($keyword){
			$query->where('title','like','%'.$keyword.'%')->orWhere('description','like','%'.$keyword.'%');
		})->list();
		return $data;
	}
	
	// 邻里二手买卖数量
	public function scopeActivityCount($query){
		$data = $query->where(['type'=>4])->count();
		return $data;
	}

	/**
	 * @param $query
	 * @return mixed
	 * 其他分享
	 */
	public function scopeOther($query,$keyword){
		$data = $query->where(['neighbor_help.type'=>5])->where(function($query) use ($keyword){
			$query->where('title','like','%'.$keyword.'%')->orWhere('description','like','%'.$keyword.'%');
		})->list();
		return $data;
	}
	
	// 邻里其他数量
	public function scopeOtherCount($query){
		$data = $query->where(['type'=>5])->count();
		return $data;
	}
	
	// 邻里其他添加
	public function scopeOtherCreate($query,$uid,$title,$description,$images,$comment_allow = 1){
		$help_id = $query->helpCreate($uid,$title,$description,$images,5,$comment_allow);
		return $help_id;
	}
	
	/**
	 * @param $query
	 * @param $uid
	 * @return mixed
	 * 邻里圈的未读分享列表
	 */
	public function scopeShareUnview($query,$uid){
		$data = $query->shareUnviewPrive($uid)->list();
		return $data;
	}
	
	// 邻里圈的未读分享数量
	public function scopeShareUnviewCount($query,$uid){
		$data = $query->shareUnviewPrive($uid);
		return $data;
	}
	
	/**
	 * @param $query
	 * @return mixed
	 * 格式化分页列表，取出对应的图片信息
	 */
	public function scopePagesParse($query){
		$data = $query->latest()->pages();
		$uid = uid();
		$data->each(function($v) use ($uid){
			$images = NeighborHelpImage::where(['help_id'=>$v->id])->pluck('image');
			// 查询自己是否点过赞
			$commended = NeighborHelpCommend::where(['uid'=>$uid,'help_id'=>$v->id])->count();
			$v->share_url = 'http://api.'.env('APP_DOMAIN').'/webview/neighbor_detail?id='.$v->id;
			$v->commended = $commended;
			$v->image = $images[0];
			$v->images = $images;
		});
		return $data;
	}
	
	// 此class私用方法
	public function scopeShareUnviewPrive($query,$uid){
		// 先找出是自己发的分享
		$help_ids = DB::table('neighbor_help')->where('uid','=',$uid)->pluck('id')->toArray();
		// 找出自己浏览过的
		$list = DB::table('neighbor_help_viewed')->where('uid','=',$uid)->pluck('help_id')->toArray();
		
		$help_not_id = array_merge($list,$help_ids);
		$query->where(function($query) use($help_not_id){
			$query->whereNotIn('id',$help_not_id);
		});
	}
	
	// 添加帮助信息公共方法
	public static function helpCreate($uid,$title,$description,$images,$type = 0,$comment_allow = 1){
		// 如果已存在
		$exist = static::where(['uid'=>$uid,'title'=>$title])->count();
		if($exist){
			throw new ApiException('已发布过该信息');
		}
		$house = ClientUserHouse::where(['uid'=>$uid])->orderBy('primary','desc')->first();
        if(!$house){
            throw new ApiException('找不到房屋信息');
        }
		$district_id = $house->village_id;
		
		$add = [
			'house_id'		=> $district_id,
			'uid'			=> $uid,
			'title'			=> $title ? : str_limit($description,30,''),
			'description'	=> $description,
			'type'			=> $type,
			'comment_allow'	=> $comment_allow,
			'status'		=> 1,		// 0待审核，1审核成功
		];
		$help_id = static::create($add)->id;
		if($images) {
               foreach ($images as $k => $v) {
                    $add_images = [
                        'help_id' => $help_id,
                        'image' => $v,
                        'defaults' => $k == 0 ? 1 : 0,
                        'status' => 1,
                    ];
                    NeighborHelpImage::create($add_images);
               }
          }
		return $help_id;
	}
}